package common.customer.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import common.customer.bean.Customer;
import common.customer.bean.ScoreDetail;
import common.customer.dao.CustomerDao;
import common.customer.dao.ScoreDetailDao;
import common.util.CommonCode;

public class CustomerServiceImpl implements CustomerService{
	private CustomerDao customerDao;
	private ScoreDetailDao scoreDetailDao;
	@Override
	public int qiandao(int customerId, String xinqing) {
		Customer customer = this.customerDao.get(customerId);
		Date zuihouqiandao = customer.getLastQiandao();
		Date now = new Date();
		Date yestoday = new Date(System.currentTimeMillis()-3600000);
		
		if(zuihouqiandao==null 
				|| (
						!new SimpleDateFormat("yyyyMMdd").format(now).equals(new SimpleDateFormat("yyyyMMdd").format(zuihouqiandao))
						&&
						!new SimpleDateFormat("yyyyMMdd").format(yestoday).equals(new SimpleDateFormat("yyyyMMdd").format(zuihouqiandao))
						)
		){
			ScoreDetail sd = new ScoreDetail();
			sd.setDate(now);
			sd.setGold(2);
			sd.setJifen(3);
			sd.setReason("签到得积分/得金币");
			sd.setUser(customer);
			this.scoreDetailDao.save(sd);
			customer.setJifen((customer.getJifen()+3));
			customer.setJinbi((customer.getJinbi()+2));
			customer.setLastQiandao(now);
			customer.setQiandao(1);
			customer.setXinqing(xinqing);
			this.customerDao.update(customer);
			return 3;
		}else if(new SimpleDateFormat("yyyyMMdd").format(new Date()).equals(new SimpleDateFormat("yyyyMMdd").format(zuihouqiandao))){
			customer.setLastQiandao(now);
			customer.setXinqing(xinqing);
			this.customerDao.update(customer);
			return 0;
		}else{
			int jifen = 0;
			int jinbi=0;
			if(customer.getQiandao()>=4){
				jifen = 15;
				jinbi = 10;
			}else{
				jifen = 3*(customer.getQiandao()+1);
				jinbi = 2*(customer.getQiandao()+1);
			}
			ScoreDetail sd = new ScoreDetail();
			sd.setDate(now);
			sd.setGold(jinbi);
			sd.setJifen(jifen);
			sd.setReason("签到得积分/得金币");
			sd.setUser(customer);
			this.scoreDetailDao.save(sd);
			customer.setJifen((customer.getJifen()+jifen));
			customer.setJinbi((customer.getJinbi()+jinbi));
			customer.setLastQiandao(now);
			customer.setQiandao((customer.getQiandao()+1));
			customer.setXinqing(xinqing);
			this.customerDao.update(customer);
			return jifen;
		}
	}
	@Override
	public boolean updateNicheng(int customerId, String nicheng) {
		Customer cust = this.customerDao.get(customerId);
		if(cust==null){
			return false;
		}
		cust.setNicheng(nicheng);
		this.customerDao.update(cust);
		return true;
	}
	@Override
	public boolean testNichengExist(int customerId, String nicheng) {
		return customerDao.testNichengExist(customerId,nicheng);
	}

	@Override
	public Customer getCustomerByUUID(String uuid) {
		return this.customerDao.getCustomerByUUID(uuid);
	}
	@Override
	public Customer getCustomerByID(int id) {
		return this.customerDao.get(id);
	}

	@Override
	public Customer getCustomerByNameAndPass(String name, String password) {
		return this.customerDao.getCustomerByNameAndPass(name, password);
	}

	@Override
	public Customer getCustomerByName(String name) {
		return this.customerDao.getCustomerByName(name);
	}
	
	@Override
	public boolean addCustomer(Customer cust) {
		int id = this.customerDao.save(cust);
		if(id>0){
			return true;
		}
		return false;
	}

	@Override
	public boolean delCustomer(Customer cust) {
		try {
			cust.setUserState(CommonCode.CUSTOMER_STATE_DELETE);
			this.customerDao.update(cust);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delCustomer(int id) {
		Customer cust = this.customerDao.get(id);
		try {
			cust.setUserState(CommonCode.CUSTOMER_STATE_DELETE);
			this.customerDao.update(cust);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean updateCustomer(Customer cust) {
		try {
			this.customerDao.update(cust);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int countCustomer() {
		return this.customerDao.countCustomer();
	}

	@Override
	public int countCustomerByCityId(int cityId) {
		return this.customerDao.countCustomerByCityId(cityId);
	}
	@Override
	public int countCustomerByName(String name) {
		return this.customerDao.countCustomerByName(name);
	}
	//========================getter and setter method
	public CustomerDao getCustomerDao() {
		return customerDao;
	}
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	public ScoreDetailDao getScoreDetailDao() {
		return scoreDetailDao;
	}
	public void setScoreDetailDao(ScoreDetailDao scoreDetailDao) {
		this.scoreDetailDao = scoreDetailDao;
	}
}
